package com.sunthy.mall.utils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 *@Description: 这是一个可以将请求参数封装成字符串
 *@Author: XM
 */
public class HttpUtils {
    /**
     * 返回请求参数的方法
     * @param request
     * @return
     * @throws IOException
     */
    public static String getRequestBody(HttpServletRequest request) throws IOException {
        // 获取请求参数
        ServletInputStream inputStream = request.getInputStream();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, length);
        }
        String requestBody = outputStream.toString("utf-8");
        outputStream.close();
        inputStream.close();
        return requestBody;
    }

    public HttpUtils() {
    }
}
